package com.base.myproject.client.HR.bcode;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxForm;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.Style.VerticalAlignment;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.HorizontalPanel;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.VerticalPanel;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.FieldSet;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.core.client.GWT;
import com.google.gwt.http.client.URL;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Label;

/**
 * @author cxghiu4102@gmail.com 宿舍查询
 * 
 */
public class RptHostel extends BaseJDBCPanel {
	BaseStore<BusiObject> store;
	PagingToolBar toolBar1 = null;

	Button search = new Button("查询");// 查询按钮
	Button export =  new Button("导出报表");
	Button rpt =  new Button("宿舍统计");

	String sql = "select hr_hostel.code, bedNumber, employno, bname, system, departmentName, bcodeRecord.sex, livedate, leavedate from hr_Hostel left join (" +
			"select 1 as bedNumber union select 2 union select 3 union select 4 union select 5 union select 6"+
			") as bed on bedNumber <= bednum left join ("+
			"select employno, bcode.bname, (select name from systeminfo where id = systemid) as system, "+
			"(select name from systeminfo where id = up_department) as departmentName, sex, livedate, leavedate, code, bednum "+
			"from bcode, bcode_job, hr_hostel_bcode "+
			"where bcode.bcode = bcode_job.bcode and bcode.bcode = hr_hostel_bcode.bcode" +
			") as bcodeRecord on bcodeRecord.code = hr_Hostel.code and bcodeRecord.bedNum = bedNumber" +
			" where isnull(liveDate, '2000-01-01') <= convert(varchar(10), getdate(), 120) and isnull(leaveDate, '2050-01-01') >= convert(varchar(10), getdate(), 120)";
	//页面通过菜单打开的默认SQL语句

	public RptHostel() {
		init();
	}

	public void complete() {
		super.complete();
		getToolbarll().add(export);
		getToolbarll().add(rpt);
		export.addListener(Events.Select, getListener());
		rpt.addListener(Events.OnClick, getListener());
	}
	public void init() {
		initWeigetx();
		registerMainStore(store);
		search.addListener(Events.OnClick, getListener());
	}

	private void initWeigetx() {
		toolBar1 = new PagingToolBar(15);

		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(5);
		FieldSet fs = new FieldSet();
		fs.setHeading("宿舍详情查询");

		fs.add(createForm());
		HorizontalPanel s = new HorizontalPanel();
		s.add(search);
		fs.add(s);

		ContentPanel panel2 = new ContentPanel();
		panel2.setHeaderVisible(false);
		panel2.add(this.createGrid());
		panel2.setBottomComponent(toolBar1);

		vp.add(fs);
		vp.add(panel2);
		add(vp);
	}

	BaseStore<BusiObject> getStore() {
		if (store == null) {
			createStore();
		}
		return store;
	}

	BaseStore<BusiObject> createStore() {
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");
		type.setTotalName("totle");
		type.addField("code");
		type.getField("code").setType(Integer.class);
		type.addField("bedNumber");
		type.getField("bedNumber").setType(Integer.class);
		type.addField("bname");
		type.getField("bname").setType(String.class);
		type.addField("employno");
		type.getField("employno").setType(String.class);
		type.addField("departmentName");
		type.getField("departmentName").setType(String.class);
		type.addField("system");
		type.getField("system").setType(String.class);
		type.addField("sex");
		type.getField("sex").setType(String.class);
		type.addField("liveDate");
		type.getField("liveDate").setType(Date.class);
		type.addField("leaveDate");
		type.getField("leaveDate").setType(Date.class);

		StoreInfo info = new StoreInfo();
		info.setTablename("hr_hostel");
		info.setKeyname(new String[] { "code" });
		
		info.setSql(sql);
		store = new BaseStore<BusiObject>(type, info);
		store.setToolBar(toolBar1);
		return store;
	}

	BaseGrid<BusiObject> grid = null;
	CheckBoxSelectionModel<BusiObject> cbs;

	BaseGrid<BusiObject> createGrid() {
		cbs = new CheckBoxSelectionModel<BusiObject>();
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();
		columns.add(cbs.getColumn());
		ColumnConfig C1 = new ColumnConfig("code", "房号", 80);
		columns.add(C1);
		
		ColumnConfig C0 = new ColumnConfig("bedNumber", "床号", 80);  
		columns.add(C0);
		
		ColumnConfig C11 = new ColumnConfig("bname", "姓名", 80);  
		columns.add(C11);
		
		ColumnConfig C4 = new ColumnConfig("employno", "工号", 120);
		columns.add(C4);
		
		ColumnConfig C2 = new ColumnConfig("departmentName", "部门", 80);  
		BaseComboBoxForm<BusiObject> bf1 = new BaseComboBoxForm<BusiObject>("id","name","SystemInfo","",true);
		C2.setRenderer(bf1);
		columns.add(C2);
		
		ColumnConfig C3 = new ColumnConfig("system", "分部", 80);
		columns.add(C3);
		
		ColumnConfig C5 = new ColumnConfig("sex", "性别", 80);
		columns.add(C5);
		
		ColumnConfig C8 = new ColumnConfig("liveDate", "入住日期", 90);
	    C8.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C8);
		
		ColumnConfig C10 = new ColumnConfig("leaveDate", "退宿日", 90);
		C10.setDateTimeFormat(DateTimeFormat.getFormat("yyyy-MM-dd"));
		columns.add(C10);
		
		ColumnModel cm = new ColumnModel(columns);
		grid = new BaseGrid<BusiObject>(getStore(), cm, false, false);
		Tools.setResize(grid, 0.99, 0.65);
		cbs.setSelectionMode(SelectionMode.SINGLE);
		grid.setSelectionModel(cbs);
		grid.setLoadMask(true);
		grid.setBorders(false);
		grid.getStore().reload();  //暂时先用reload，因为不reload的话，分面不出来效果，但是这样数据多了会慢。再想解决方法
		return grid;
	}

	FormPanel form;
	DateField endDate = new DateField(); // 签订结束日期
	TextField<String> code = new TextField<String>();

	private FormPanel createForm() {
		endDate.getPropertyEditor().setFormat(
				DateTimeFormat.getFormat("yyyy-MM-dd"));
		
		form = new FormPanel();
		TableLayout tl = new TableLayout(8);
		form.setWidth(913);
		form.setPadding(4);
		form.setBodyBorder(false);
		form.setHeaderVisible(false);
		TableData td = new TableData();
		td.setRowspan(1);
		td.setHorizontalAlign(HorizontalAlignment.LEFT);
		td.setVerticalAlign(VerticalAlignment.TOP);
		td.setPadding(2);

		form.add(new Label("住宿到期日:"), td);
		endDate.setValue(new Date());
		form.add(endDate, td);

		form.add(new Label("宿舍号:"), td);
		form.add(code, td);

		form.setLayout(tl);

		return form;
	}

	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}

		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				String endTime = endDate.getRawValue();
				String c = code.getRawValue();
				
				if (endTime.equals("")) {
					MessageBox.alert("提示", "住宿到期日期不能为空，请输入宿舍到期日期，谢谢", null);
					return;
				}
				
				String searchSql = "select hr_hostel.code, bedNumber, employno, bname, system, departmentName, bcodeRecord.sex, livedate, leavedate from hr_Hostel left join (" +
							"select 1 as bedNumber union select 2 union select 3 union select 4 union select 5 union select 6"+
							") as bed on bedNumber <= bednum left join ("+
							"select employno, bcode.bname, (select name from systeminfo where id = systemid) as system, "+
							"(select name from systeminfo where id = up_department) as departmentName, sex, livedate, leavedate, code, bednum "+
							"from bcode, bcode_job, hr_hostel_bcode "+
							"where bcode.bcode = bcode_job.bcode and bcode.bcode = hr_hostel_bcode.bcode ) as bcodeRecord on bcodeRecord.code = hr_Hostel.code and bcodeRecord.bedNum = bedNumber where 1=1";
				StringBuilder where = new StringBuilder();
				if (!endTime.equals("")) {
					where.append(" and isnull(liveDate, '2000-01-01') <= '");
					where.append(endTime);
					where.append("' and isnull(leaveDate, '2050-01-01') >= '");
					where.append(endTime);
					where.append("'");
				}
				
				if (!c.equals("")) {
					where.append(" and hr_Hostel.code = '");
					where.append(c);
					where.append("'");
				}
					
				if(be.getSource().equals(search)){
					searchSql += where + "";
					grid.getStore().getStoreinfo().setSql(searchSql);
					grid.getStore().reload();
				} else if (be.getSource().equals(export)) {
					StringBuilder data = new StringBuilder();
					data.append("where=");
					data.append(where.toString());
					data.append("&excel=1");
		
					Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptHostle.jsp?" + URL.encode(data.toString()), "_blank", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
				} else if (be.getSource().equals(rpt)) {
					StringBuilder data = new StringBuilder();
					data.append("where=");
					data.append(where.toString());
					Window.open(GWT.getHostPageBaseURL() + "hr_Jsp/rptSumHostel.jsp?" + URL.encode(data.toString()), "_blank", "menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes");
				}
			}
	
	};
	
		return defaultlistener;
	}
}
